package cz.khfotbal.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cz.khfotbal.dao.CompetionDao;
import cz.khfotbal.domain.Competion;

@Repository("competionDaoImpl")
@Transactional
public class CompetionDaoImpl implements CompetionDao {

	@Autowired
	private SessionFactory sessionFactory;

	private Session session;

	public void saveCompetion(Competion competion) {
		session = sessionFactory.getCurrentSession();
		session.saveOrUpdate(competion);
	}

	public List<Competion> findAllCompetions() {
		session = sessionFactory.getCurrentSession();
		List<Competion> list = session.createQuery("from Competion").list();
		return list;
	}

	public void updateCompetion(Competion competion) {
		session = sessionFactory.getCurrentSession();
		session.merge(competion);

	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Competion findCompetionById(int competionId) {
		session = sessionFactory.getCurrentSession();
		Competion result = (Competion) session.createQuery(
				"from Competion c where c.id=" + competionId).uniqueResult();
		return result;
	}

	public void deleteCompetion(Competion competion) {
		session = sessionFactory.getCurrentSession();
		session.delete(competion);
	}

}
